package br.edu.ufcg.dsc.actions.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import br.edu.ufcg.dsc.domain.Library;
import br.edu.ufcg.dsc.domain.User;
import br.edu.ufcg.dsc.domain.UserType;
import br.edu.ufcg.dsc.services.LibraryService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@Result(name = "success", type = ServletDispatcherResult.class, value = "/jsp/user/userDetails.jsp")
@ParentPackage(value = "myPackage")
public class FindUserAction extends BaseUserAction implements Preparable{

	private static final long serialVersionUID = 8161395602084802160L;

	private List<String> librariesNames, typesAdmin, typesLibrarian;
	private List<Library> userLibraries;
	
	public void prepare() throws Exception{
		super.prepare();
		typesAdmin = new ArrayList<String>();
		typesAdmin.add(UserType.ADMIN.name());
		typesAdmin.add(UserType.LIBRARIAN.name());

		typesLibrarian = new ArrayList<String>();
		typesLibrarian.add(UserType.STUDENT.name());
		typesLibrarian.add(UserType.TEACHER.name());
		
		librariesNames = new ArrayList<String>();

		User user = (User) ActionContext.getContext().getSession().get("user");
		userLibraries = user.getLibraries();
	}

	public List<String> getLibraries(String top){
		if (top.equals(UserType.LIBRARIAN.name())){
			librariesNames.clear();
			List<Library> libraries = LibraryService.findAll();
			for(int i = 0; i < libraries.size(); i++)
				librariesNames.add(libraries.get(i).getName());
			return librariesNames;
		}
		librariesNames.clear();
		librariesNames.add("-- none --");
		return librariesNames;
	}

	public List<String> getTypesAdmin() {
		return typesAdmin;
	}

	public void setTypesAdmin(List<String> typesAdmin) {
		this.typesAdmin = typesAdmin;
	}
	
	public List<String> getTypesLibrarian() {
		return typesLibrarian;
	}

	public void setTypesLibrarian(List<String> typesLibrarian) {
		this.typesLibrarian = typesLibrarian;
	}

	public List<Library> getUserLibraries() {
		return userLibraries;
	}

	public void setUserLibraries(List<Library> userLibraries) {
		this.userLibraries = userLibraries;
	}
}